<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="120px" v-show="showSearch">
            <el-form-item label="处置要点类型" prop="dmType">
                <el-select v-model="queryParams.dmType" placeholder="请选择处置要点类型" clearable>
                    <el-option v-for="item in optionType" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="标签" prop="dmLabel">
                <el-input v-model="queryParams.dmLabel" placeholder="请输入标签" clearable />
            </el-form-item>
            <el-form-item label="子标签" prop="dmSubtitle">
                <el-input v-model="queryParams.dmSubtitle" placeholder="请输入子标签" clearable />
            </el-form-item>
            <el-form-item label="活动名称" prop="iaName">
                <el-input v-model="queryParams.iaName" placeholder="请输入活动名称" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleQuery">搜索</el-button>
                <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" @click="handleUpload">
                    <svg-icon class-name="upload-icon" icon-class="upload" /><span style="margin-left:10px">上传</span>
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" icon="Edit" @click="handleTag(1)">标签管理</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" icon="Edit" @click="handleSubTag(1)">子标签管理</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList" stripe @selection-change="handleSelectionChange" row-key="dmId">
            <el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
            <el-table-column label="标签" align="center" prop="dmLabel" />
            <el-table-column label="子标签" align="center" prop="dmSubtitle" />
            <el-table-column label="处置要点类型" align="center" prop="dmType" />
            <el-table-column label="管理员" align="center" prop="admin" />
            <el-table-column label="上传时间" align="center" prop="uploadTime" />
            <el-table-column label="活动名称" align="center" prop="iaName" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>
                    <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
            @pagination="getList" />

        <el-dialog title="处置要点管理" v-model="open" width="600px" top="200px" append-to-body>
            <el-form ref="czydRef" label-width="120px" :model="form" :rules="rules">
                <el-row style="margin: 20px 0;">
                    <el-col :span="24">
                        <el-form-item label="处置要点类型" prop="dmType">
                            <el-select v-model="form.dmType" placeholder="请选择处置要点类型" clearable>
                                <el-option v-for="item in optionType" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="14.5">
                        <el-form-item label="标签" prop="dmLabel">
                            <el-input v-model="form.dmLabel" placeholder="请选择标签" :readonly="true" clearable @click="handleTag" >
                                <template #suffix>
                                    <el-icon class="el-input__icon">
                                        <Plus />
                                    </el-icon>
                                </template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="14.5">
                        <el-form-item label="子标签" prop="dmSubtitle">
                            <el-input v-model="form.dmSubtitle" placeholder="请选择标签" :readonly="true" clearable @click="handleSubTag" >
                                <template #suffix>
                                    <el-icon class="el-input__icon">
                                        <Plus />
                                    </el-icon>
                                </template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="灾害风险点" prop="disasterRisk">
                            <el-input v-model="form.disasterRisk" type="textarea" maxlength="1000" show-word-limit
                                :autosize="{ minRows: 5, maxRows: 5 }" placeholder="请输入灾害风险点" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="处置要点" prop="disposalPoint">
                            <el-input v-model="form.disposalPoint" type="textarea" maxlength="1000" show-word-limit
                                :autosize="{ minRows: 5, maxRows: 5 }" placeholder="请输入处置要点" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">保 存</el-button>
                    <el-button @click="open = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>

        <el-dialog title="标签管理" v-model="openTagManage" width="600px" append-to-body>
            <el-row style="margin: 20px 0;">
                <el-col :span="16">
                    <el-input v-model="formData.label" type="text" placeholder="请输入标签" clearable />
                </el-col>
                <el-col :span="8">
                    <el-button type="primary" @click="getTag" style="margin-left:15px">搜索</el-button>
                    <el-button type="primary" @click="resetTag">重置</el-button>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="16">
                    <el-input v-model="dmLabel" type="text" placeholder="请输入标签" clearable />
                </el-col>
                <el-col :span="8">
                    <el-button type="primary" @click="handleAddTag" :disabled="!dmLabel" style="margin-left:15px">添加</el-button>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-table v-loading="loading" ref="tagRef" :data="tagList" stripe style="margin-top:20px" @select="select" @selection-change="selectionChange" @select-all="onSelectAll" row-key="id">
                        <el-table-column type="selection" width="55" align="center" :reserve-selection="true" v-if="!isGl"/>
                        <el-table-column label="标签" align="center" prop="label" />
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                            <template #default="scope">
                                <el-button type="text" @click="deleteTag(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :total="total1"  v-show="total1 > 0" v-model:page="formData.page" v-model:limit="formData.rows"
                        @pagination="getTag" />
                </el-col>
            </el-row>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="confirmSelect" style="width: 20px;height: 30px;font-size: 14px;" v-if="!isGl" :disabled="!tagLable">确 定</el-button>
                    <el-button @click="cancelTag" style="width: 20px;height: 30px;font-size: 14px;">关 闭</el-button>
                </div>
            </template>
        </el-dialog>

        <el-dialog title="子标签管理" v-model="openSubTagManage" width="600px" append-to-body>
            <el-row style="margin: 20px 0;">
                <el-col :span="16">
                    <el-input v-model="formData1.subtitle" type="text" placeholder="请输入子标签" clearable />
                </el-col>
                <el-col :span="8">
                    <el-button type="primary" @click="getSubTag" style="margin-left:15px">搜索</el-button>
                    <el-button type="primary" @click="resetSubTag">重置</el-button>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="16">
                    <el-input v-model="dmSubLabel" type="text" placeholder="请输入子标签" clearable />
                </el-col>
                <el-col :span="8">
                    <el-button type="primary" @click="handleAddSubTag" :disabled="!dmSubLabel" style="margin-left:15px">添加</el-button>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-table v-loading="loading" ref="tagSubRef" :data="subTagList" stripe style="margin-top:20px"  @select="selectSub" @selection-change="selectionChangeSub" @select-all="onSelectAll" row-key="id">
                        <el-table-column type="selection" width="55" align="center" :reserve-selection="true" v-if="!isSubGl"/>
                        <el-table-column label="子标签" align="center" prop="subtitle" />
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                            <template #default="scope">
                                <el-button type="text" @click="deleteSubTag(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :total="total2"  v-show="total2 > 0" v-model:page="formData1.page" v-model:limit="formData1.rows"
                        @pagination="getSubTag" />
                </el-col>
            </el-row>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="confirmSubSelect" style="width: 20px;height: 30px;font-size: 14px;" v-if="!isSubGl" :disabled="!subTagLable">确 定</el-button>
                    <el-button @click="cancelSubTag" style="width: 20px;height: 30px;font-size: 14px;">关 闭</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
  
<script setup name="disposalPoint">
import { getAllList, getAllTag, getAllSubTag, saveTag, delTag,saveSubTag,delSubTag,saveDisposeManager, delDisposeManager, getDisposeManager , updateDisposeManager} from "@/api/auxiliaryMeasures/disposalPoint.js"
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const tagList = ref([]);
const subTagList = ref([]);
const ids = ref();
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const total1 = ref(0);
const total2 = ref(0);
const showSearch = ref(true);
const optionType = [{ label: '物资储备', value: '物资储备' }, { label: '房屋破坏', value: '房屋破坏' }, { label: '地震灾害', value: '地震灾害' }, { label: '应急疏散', value: '应急疏散' }, { label: '其他', value: '其他' }]
const open = ref(false);
const openTag = ref(false);
const openSubTag = ref(false);
const isAdd = ref(true);
const tagId = ref();
const tagFormId = ref();
const tagLable = ref();
const subTagId = ref();
const subTagFormId = ref();
const subTagLable = ref();

const dmLabel = ref();
const dmSubLabel = ref();

const openTagManage =ref(false);
const openSubTagManage =ref(false);
const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
        dmType: undefined,
        dmLabel: undefined,
        dmSubtitle: undefined,
        iaName:undefined,
    },
    queryParams1: {
        dmType: undefined,
        dmLabel: undefined,
        dmSubtitle: undefined,
        iaName:undefined,
    },
    formData:{
        page:1,
        rows:10,
        label:undefined,
    },
    formData1:{
        page:1,
        rows:10,
        subtitle:undefined,
    },
    form: {
        dmType: undefined,
        dmLabel: undefined,
        dmSubtitle: undefined,
        disasterRisk: undefined,
        disposalPoint: undefined,
    },
    rules: {
        dmType: [{ required: true, message: "处置要点类型不能为空", trigger: "change" }],
        dmLabel: [{ required: true, message: "标签不能为空", trigger: "change" }],
        disasterRisk: [{ required: true, message: "灾害风险点不能为空", trigger: "blur" }],
        disposalPoint: [{ required: true, message: "处置要点不能为空", trigger: "blur" }],
    }
})
const { queryParams,queryParams1, form, rules,formData,formData1 } = toRefs(data)


/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    dataList.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 查询数据列表 */
function getList() {
    loading.value = true;
    getAllList(queryParams.value).then(response => {
        dataList.value = response.data.disposeManagers ? response.data.disposeManagers : [];
        total.value = response.data.total;
        loading.value = false;
    });
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.dmId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 删除按钮操作 */
function handleDelete(row) {
    const id = row.dmId || ids.value;
    proxy.$modal.confirm('是否确认删除选中的数据项?').then(function () {
        return delDisposeManager(id);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

// 上传 
function handleUpload() {
    isAdd.value = true;
    proxy.resetForm("czydRef");
    open.value = true;
}

function submitForm() {
    const query ={...form.value,labelId:tagFormId.value,subtitleId:subTagFormId.value}
    proxy.$refs["czydRef"].validate(valid => {
        if (valid) {
            if(isAdd.value){
                saveDisposeManager(query).then(res => {
                getList();
                proxy.$modal.msgSuccess("上传成功");
                open.value = false;
            })
            }else{
                updateDisposeManager(query).then(res => {
                getList();
                proxy.$modal.msgSuccess("修改成功");
                open.value = false;
            })
            }
        }
    })
}
// 添加标签
function handleAddTag(){
    saveTag({label:dmLabel.value}).then(res=>{
        getTag();
        proxy.$modal.msgSuccess("添加成功");
    })
}
function handleAddSubTag(){
    saveSubTag({subtitle:dmSubLabel.value}).then(res=>{
        getSubTag();
        proxy.$modal.msgSuccess("添加成功");
    })
}
// 取消标签管理
function cancelTag(){
    dmLabel.value = undefined;
    openTagManage.value = false;
}
function cancelSubTag(){
    dmSubLabel.value = undefined;
    openSubTagManage.value = false;
}
// 删除
function deleteTag(row){
    delTag(row.id).then(res=>{
        getTag();
        proxy.$modal.msgSuccess("删除成功");
    })
}
function deleteSubTag(row){
    delSubTag(row.id).then(res=>{
        getSubTag();
        proxy.$modal.msgSuccess("删除成功");
    })
}
// 修改
function handleEdit(row) {
    isAdd.value = false;
    proxy.resetForm("czydRef");
    handleDetail(row.dmId)
    open.value = true;
}

// 查
function handleDetail(id) {
    getDisposeManager(id).then(res=>{
        form.value = {
            dmType: res.data.dmType,
            dmLabel: res.data.dmLabel,
            dmSubtitle: res.data.dmSubtitle,
            disasterRisk: res.data.disasterRisk,
            disposalPoint: res.data.disposalPoint,
            dmId: res.data.dmId,
        };
        tagFormId.value = res.data.labelId;
        subTagFormId.value = res.data.subtitleId;
    })
}
const isGl = ref(false);
const isSubGl = ref(false);
function handleTag(num) {
    if(num==1) {
        isGl.value = true
    }else{
        isGl.value = false
    };
    formData.value = {
        page:1,
        rows:10,
        label:undefined
    }
    getTag();
    openTagManage.value = true;
    tagLable.value = undefined;
    tagId.value = undefined;
    proxy.$refs['tagRef'].clearSelection();
}
function handleSubTag(num) {
    if(num==1) {
        isSubGl.value = true
    }else{
        isSubGl.value = false
    };
    formData1.value = {
        page:1,
        rows:10,
        subtitle:undefined
    }
    // getTag();
    getSubTag();
    openSubTagManage.value = true;
    subTagLable.value = undefined;
    subTagId.value = undefined;
    proxy.$refs['tagSubRef'].clearSelection();
}

function select(selection,row){
    proxy.$refs['tagRef'].clearSelection();
    if(selection.length==0) return
    proxy.$refs['tagRef'].toggleRowSelection(row, true);
}

function selectionChange(val){
    tagLable.value = val.length>0?val[0].label:undefined;
    tagId.value = val.length>0?val[0].id:undefined;
}

function onSelectAll(){
    proxy.$refs['tagRef'].clearSelection();
    proxy.$refs['tagSubRef'].clearSelection();
}
function confirmSelect(){
    form.value.dmLabel = tagLable.value;
    tagFormId.value = tagId.value;
    openTagManage.value = false;
}

function selectSub(selection,row){
    proxy.$refs['tagSubRef'].clearSelection();
    if(selection.length==0) return
    proxy.$refs['tagSubRef'].toggleRowSelection(row, true);
}

function selectionChangeSub(val){
    subTagLable.value = val.length>0?val[0].subtitle:undefined;
    subTagId.value = val.length>0?val[0].id:undefined;
}

function confirmSubSelect(){
    form.value.dmSubtitle = subTagLable.value;
    subTagFormId.value = subTagId.value;
    openSubTagManage.value = false;
}

function getTag() {
    getAllTag(formData.value).then(res => {
        tagList.value = res.data.rows ? res.data.rows : [];
        total1.value = res.data.total;
    })
}
function resetTag(){
    formData.value.label = undefined;
    formData.value.page = 1;
    getTag();
}
function getSubTag() {
    getAllSubTag(formData1.value).then(res => {
        subTagList.value = res.data.rows ? res.data.rows : [];
        total2.value = res.data.total;
    })
}
function resetSubTag(){
    formData1.value.subtitle = undefined;
    formData1.value.page = 1;
    getSubTag()
}

getList();
</script>
<style lang="scss" scoped>
.active{
color: #107ce5;
}

:deep(.el-radio-group){
  display: flex;
  align-items: flex-start;
  flex-direction: column;
}
</style>